package com.archgeek.java.bytebuffer;

import org.junit.Test;

import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.channels.Channels;
import java.nio.charset.StandardCharsets;

/**
 * @author pizhihui
 * @date 2024-06-14 13:39
 */
public class ByteBufferDemo {


    @Test
    public void test1() {

        // =============== 分配
        ByteBuffer allocate = ByteBuffer.allocate(16 * 1024);
        System.out.println("------------ allocate ");
        System.out.println("position: " + allocate.position());
        System.out.println("limit:    " + allocate.limit());
        System.out.println("capacity: " + allocate.capacity());

        // =============== 写入数据
        byte[] data = "hello world".getBytes(StandardCharsets.UTF_8);
        allocate.put(data, 0, data.length);
        System.out.println("------------ put data ");
        System.out.println("position: " + allocate.position());
        System.out.println("limit:    " + allocate.limit());
        System.out.println("capacity: " + allocate.capacity());

        // =============== 切换成读数据模式
        allocate.flip();
        System.out.println("------------ flip ");
        System.out.println("position: " + allocate.position());
        System.out.println("limit:    " + allocate.limit());
        System.out.println("capacity: " + allocate.capacity());


        // =============== 读取数据
        byte[] read = new byte[5];
        allocate.get(read, 0, read.length);
        System.out.println("------------ read ");
        System.out.println("data:     " + new String(read));
        System.out.println("position: " + allocate.position());
        System.out.println("limit:    " + allocate.limit());
        System.out.println("capacity: " + allocate.capacity());


        // =============== 重复读
        allocate.rewind();
        System.out.println("------------ rewind ");
        System.out.println("data:     " + new String(read));
        System.out.println("position: " + allocate.position());
        System.out.println("limit:    " + allocate.limit());
        System.out.println("capacity: " + allocate.capacity());


        // =============== 清除数据
        allocate.clear();
        System.out.println("------------ clear ");
        System.out.println("position: " + allocate.position());
        System.out.println("limit:    " + allocate.limit());
        System.out.println("capacity: " + allocate.capacity());


        // =============== mark
    }

    @Test
    public void test2() {
        ByteBuffer allocate = ByteBuffer.allocate(16 * 1024);
        byte[] data = "helloworld".getBytes(StandardCharsets.UTF_8);
        allocate.put(data, 0, data.length);

        System.out.println("capacity:" + allocate.capacity() + ",position:" + allocate.position() + ",limit:" + allocate.limit());


        allocate.flip();

        byte[] read = new byte[16384];
        allocate.get(read, 0, 2);
        System.out.println("data1====" + new String(read));
        System.out.println("capacity:" + allocate.capacity() + ",position:" + allocate.position() + ",limit:" + allocate.limit());


        allocate.mark();

        allocate.get(read, 2, 2);
        System.out.println("data1====" + new String(read));
        System.out.println("capacity:" + allocate.capacity() + ",position:" + allocate.position() + ",limit:" + allocate.limit());

        allocate.reset();


        allocate.get(read, 2, 2);
        System.out.println("data1====" + new String(read));
        System.out.println("capacity:" + allocate.capacity() + ",position:" + allocate.position() + ",limit:" + allocate.limit());
        System.out.println("capacity:" + allocate.capacity() + ",position:" + allocate.position() + ",limit:" + allocate.limit());

    }

    @Test
    public void testHasRemaing() {

        ByteBuffer size = ByteBuffer.allocate(4);
        if (size.hasRemaining()) {
            System.out.println("还没有写完成");
        }
        System.out.println("capacity:" + size.capacity() + ",position:" + size.position() + ",limit:" + size.limit());

        // byte[] headData = new byte[4];
        size.putInt(199);
        System.out.println("capacity:" + size.capacity() + ",position:" + size.position() + ",limit:" + size.limit());

        if (!size.hasRemaining()) {
            System.out.println("还没有读写完成");
        }

        size.rewind();
//        size.flip();
        System.out.println("capacity:" + size.capacity() + ",position:" + size.position() + ",limit:" + size.limit());

        int dataSize = size.getInt();

        ByteBuffer data = ByteBuffer.allocate(dataSize);

        System.out.println("capacity:" + data.capacity() + ",position:" + data.position() + ",limit:" + data.limit());



    }


    public static void main(String[] args) {


    }

}
